<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>商品修改</title>
    <th:block th:replace="layout :: resource"></th:block>
</head>
<body class="zjgl-layout-page">
<div class="layui-container">
    <form class="layui-form" id="search-form">
        <div class="layui-form-item">
            <label class="layui-form-label">商品名</label>
            <div class="layui-input-inline">
                <label>
                    <input type="text" name="name"  required lay-verify="required"
                           placeholder="请输入商品名" lay-reqText="商品名不能为空！" autocomplete="off" class="layui-input" style="width: 160px">
                </label>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">售价</label>
            <div class="layui-input-inline">
                <input type="text" name="price"  required lay-verify="required"
                       placeholder="请输入售价" lay-reqText="售价不能为空！" autocomplete="off" class="layui-input" style="width: 160px">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">库存</label>
            <div class="layui-input-inline">
                <input type="text" name="number" required lay-verify="required"
                       placeholder="请输入库存" lay-reqText="库存不能为空！" autocomplete="off" class="layui-input" style="width: 160px">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">成本</label>
            <div class="layui-input-inline">
                <input type="text" name="cost"  required lay-verify="required"
                       placeholder="请输入成本" lay-reqText="成本不能为空！" autocomplete="off" class="layui-input" style="width: 160px">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">类型</label>
            <div class="layui-input-inline">
                <input type="text" name="categoryId"  required lay-verify="required"
                       placeholder="请输入类型" lay-reqText="类型不能为空！" autocomplete="off" class="layui-input" style="width: 160px">
            </div>
        </div>
        <div class="layui-inline" style="padding-left: 50px">
            <button class="layui-btn" lay-submit lay-filter="save" id="save">
                <i class="flaticon2-magnifier-tool"></i> 保存
            </button>
            <button type="reset" class="layui-btn  layui-btn-primary" id="btnReset">
                重置
            </button>
        </div>
    </form>
</div>
<script th:inline="javascript">

    var pageObj={
        addUrl:"/goods/api/add",
    }

    var admin = {
        id: null,
        name: null,
        price: null,
        cost: null,
        categoryId: null,
        number: null,
        adminId: null
    };

    layui.$(function () {
        //表单提交
        layui.use('form', function () {
            var form = layui.form;
            form.on('submit(save)', function (data) {
                pageObj.name = data.field.name;
                pageObj.price = data.field.price;
                pageObj.cost = data.field.cost;
                pageObj.categoryId = data.field.categoryId;
                pageObj.number = data.field.number;
                pageObj.adminId = data.field.adminId;

                $.ajax({
                    url: pageObj.addUrl,
                    method: "POST",
                    contentType: "application/json",
                    dataType: "json",
                    data: JSON.stringify(data.field),
                    success: function (result) {
                        if (result.code === 0) {
                            layer.msg("添加成功", {shade: 0.3, time: 2000, icon: 1});
                            setTimeout(function () {
                                parent.location.reload();
                            },1000);
                        } else {
                            layer.msg(result.msg, {shade: 0.3, time: 2000, icon: 5});
                        }
                    },
                    error: function (responseData) {
                        layer.msg('保存失败，请联系管理员！', {shade: 0.3, time: 2000, icon: 5});
                    },
                });
                return false;
            });
        });
    });
</script>
</body>
</html>